-- BonusInfo
-- Create by weism
-- 奖励信息

BonusInfo = {
    type = 1,   -- 1：物品奖励   2：属性奖励
    amount = 1, -- 奖励的数量
};
BonusInfo.__index = BonusInfo;

-- 构造函数
function BonusInfo.new(desc)
    local self = {};
    setmetatable(self, BonusInfo);

    if string.startWith(desc, "1(") then
        -- 这是物品奖励
        self.type = 1;
        local arr = string.explode(string.sub(desc, 3, -1), ",");
        self.classId = tonumber(arr[1]);
        self.amount = tonumber(arr[2]);
    elseif string.startWith(desc, "2(") then
        -- 这是属性奖励
        self.type = 2;
        local arr = string.explode(string.sub(desc, 3, -2), ",");
        self.attrib = arr[1];
        self.amount = tonumber(arr[2]);
    else
        error("错误的奖励配置");
    end

    return self;
end

-- 取得该对象的描述字符串
function BonusInfo:get_ob_id()
    return string.format("BonusInfo:%s", tostring(self.type));   
end

-- 发放奖励
function BonusInfo:doBonus()
    if (self.type == 1) then
        -- 奖励N个物品
        local c = self.amount;
        local items = ME.user.items;
        if (items[self.classId] ~= nil) then
            c = items[self.classId].amount + c;
        end
        ItemM.updateItem(ME.user, self.classId, c);
        
        local msg = TextColorM.appendSkyBlue("【胜利】");
        msg = msg .. string.format("你获得了%s", TextColorM.appendYellow(ItemM.query(self.classId, "name") or ""));
        UIRoom.getInstance().combatCtr.combatText:addNewLine(msg);
        
        -- 添加统计信息
        CombatStatM.addItem(self.classId, self.amount);
    else
        -- 奖励金钱，其他属性待添加
        local c = ME.user.dbase:query("money", 0);
        ME.user.dbase:set("money", c + self.amount);
        
        local msg = TextColorM.appendSkyBlue("【胜利】");
        msg = msg .. string.format("你获得了%s枚金币", TextColorM.appendGolden(self.amount));
        UIRoom.getInstance().combatCtr.combatText:addNewLine(msg);
        
        -- 添加统计信息
        CombatStatM.addMoney(self.amount);
    end
end
